package ua.mihaylenko.app.data.assets 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.text.Font;
	import flash.utils.Dictionary;
	import ua.mihaylenko.app.controller.APPStage;
	import ua.mihaylenko.app.data.AppData;
	import ua.mihaylenko.app.ui.preloader.UIPreloader;
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class FontsManager extends EventDispatcher
	{
		private var _cData:FontsManagerData;
		private var _cPreloader:UIPreloader;
		private var _cFontsDomain:ApplicationDomain;
		private var _cLoader:Loader;
		private var _dctFonts:Dictionary;
		public function FontsManager() 
		{
			
		}
		public function innit($preloader:UIPreloader):void
		{
			_cData = AppData.instance.getData(FontsManagerData) as FontsManagerData;
			
			_cPreloader = $preloader;
			_cPreloader.title = _cData.preloaderMsg;
			
			_cFontsDomain = new ApplicationDomain();
			_cLoader = new Loader();
			_cLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontsLoaded);
			_cLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);
			_cLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			try{
				_cLoader.load(new URLRequest(_cData.url), new LoaderContext(true, _cFontsDomain, AppData.instance.isLocal?null:SecurityDomain.currentDomain));
			}catch (e:Error) {
				onError();
			}
		}
		private function onError(e:Event=null):void
		{
			_cLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fontsLoaded);
			_cLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, showProgress);
			_cLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_cLoader = null;
			APPStage.error('Error load fonts data');
		}
		private function showProgress(e:ProgressEvent):void 
		{
			if (_cPreloader)_cPreloader.onProgress(e);
		}
		
		private function fontsLoaded(e:Event):void 
		{
			_cLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fontsLoaded);
			_cLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, showProgress);
			_cLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			_dctFonts = new Dictionary();
			for (var iFont:int = 0; iFont < _cData.maxId; ++iFont ) {
				try{
					var cFontClass:Class = _cFontsDomain.getDefinition(_cData.lnkId + iFont.toString()) as Class;
					if (cFontClass && cFontClass is Font) {
						Font.registerFont(cFontClass);
						_dctFonts[iFont] = cFontClass;
					}
				}catch (e:Error) {
					trace('Not found font, id=', _cData.lnkId + iFont.toString());
				}
			}
			_cPreloader = null;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function getFontById(iId:int):Font {
			var cFontClass:Class = _dctFonts[iId];
			if (cFontClass) return new cFontClass();
			return null;
		}
	}

}